package org.pub.fast.jdbc.cross.adapter;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.pub.fast.jdbc.cross.CrossDBPreparedStatement;

public class SQLServerAdapter  extends BaseAdapter {
	public String getName() {
		return getClass().getName();
	}

	public String getDriverClass() {
		return "com.microsoft.jdbc.sqlserver.SQLServerDriver";
	}

	public Object getObject(ResultSet rs, int columnIndex,int scale) throws SQLException {
		Object value = super.getObject(rs, columnIndex,  scale);
		if (value instanceof Short)
			return Integer.valueOf(String.valueOf(value));
		return value;
	}

	public Object getObject(ResultSet rs, String columnName,int scale)
			throws SQLException {
		Object value = super.getObject(rs, columnName, scale);
		if (value instanceof Short)
			return Integer.valueOf(String.valueOf(value));
		return value;
	}

	public void init() throws SQLException {
	}

	public String getBinaryConstant(String s) {
		return "0x" + s;
	}

	public String getNow() throws SQLException {
		return "select GETDATE() value ";
	}

	public void setNull(CrossDBPreparedStatement prep, int parameterIndex,
			int sqlType) throws SQLException {
		if (sqlType == Types.CLOB) {
			((PreparedStatement) prep.getVendorObject()).setNull(
					parameterIndex, Types.VARCHAR);
		} else if (sqlType == Types.BLOB) {
			((PreparedStatement) prep.getVendorObject()).setNull(
					parameterIndex, Types.BINARY);
		} else {
			((PreparedStatement) prep.getVendorObject()).setNull(
					parameterIndex, sqlType);
		}
	}

}
